package com.black.auth.client.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.black.auth.client.mapper.OauthClientDetailsMapper;
import com.black.auth.client.po.OauthClientDetails;
import com.black.auth.client.service.OauthClientDetailsService;
import com.black.common.web.PageQuery;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author linwenfeng
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class OauthClientDetailsServiceImpl extends ServiceImpl<OauthClientDetailsMapper, OauthClientDetails> implements OauthClientDetailsService {

    private final PasswordEncoder passwordEncoder;

    @Override
    public IPage<OauthClientDetails> search(PageQuery<OauthClientDetails> searchVo) {
        LambdaQueryWrapper<OauthClientDetails> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(searchVo.getQueryData().getClientId())) {
            queryWrapper.eq(OauthClientDetails::getClientId, searchVo.getQueryData().getClientId());
        }
        if (!StringUtils.isEmpty(searchVo.getQueryData().getAuthorizedGrantTypes())) {
            queryWrapper.like(OauthClientDetails::getAuthorizedGrantTypes, searchVo.getQueryData().getAuthorizedGrantTypes());
        }
        IPage<OauthClientDetails> result = this.page(searchVo.getPager(), queryWrapper);
        List<OauthClientDetails> records = new ArrayList<>();
        result.getRecords().forEach(o -> {
            o.setClientSecret(null);
            records.add(o);
        });
        result.setRecords(records);
        return result;
    }

    @Override
    public OauthClientDetails findById(String clientId) {
        OauthClientDetails oauthClientDetails = this.baseMapper.selectById(clientId);
        oauthClientDetails.setClientSecret(null);
        return oauthClientDetails;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createOauthClientDetails(OauthClientDetails oauthClientDetails) throws Exception {
        OauthClientDetails byId = this.baseMapper.selectById(oauthClientDetails.getClientId());
        if (byId != null) {
            throw new Exception("该Client已存在");
        }
        oauthClientDetails.setClientSecret(passwordEncoder.encode(oauthClientDetails.getClientSecret()));
        this.save(oauthClientDetails);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateOauthClientDetails(OauthClientDetails oauthClientDetails) {
        LambdaQueryWrapper<OauthClientDetails> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OauthClientDetails::getClientId, oauthClientDetails.getClientId());
        oauthClientDetails.setClientId(null);

        this.update(oauthClientDetails, queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteOauthClientDetails(List<String> clientIds) {
        LambdaQueryWrapper<OauthClientDetails> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(OauthClientDetails::getClientId, clientIds.toArray());
        this.remove(queryWrapper);
    }
}
